package com.cskaoyan.controller.authController;


import com.cskaoyan.aspect.LogA;
import com.cskaoyan.config.MallToken;
import com.cskaoyan.exceptionJY.WrongPasswordException;
import com.cskaoyan.model.bo.LoginBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.systemvo.AuthInfoVo;
import com.cskaoyan.service.SystemService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthController {

    @Autowired
    SystemService systemService;

    @RequestMapping("admin/auth/login")
    @LogA
    public BaseRespVo login(@RequestBody LoginBo loginBo) throws WrongPasswordException {
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new MallToken(loginBo.getUsername(),loginBo.getPassword(),"admin"));
        } catch (AuthenticationException e) {
            //验证未通过
            throw new WrongPasswordException();
        }
        //验证通过
        return BaseRespVo.ok(subject.getSession().getId());
    }

    @RequestMapping("admin/auth/info")
    public BaseRespVo info(String token){
        AuthInfoVo authInfoVo = systemService.authInfo(token);
        return BaseRespVo.ok(authInfoVo);
//        LoginData loginData = new LoginData();
//        loginData.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
//        loginData.setName("超级管理员");
//        ArrayList<String> perms = new ArrayList<>();
//        perms.add("*");
//        loginData.setPerms(perms);
//
//        ArrayList<String> roles = new ArrayList<>();
//        roles.add("超级管理员");
//        loginData.setRoles(roles);
//
//        return BaseRespVo.ok(loginData);
    }

    @RequestMapping("admin/auth/logout")
    @LogA
    public BaseRespVo logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }

    @RequestMapping("unAuthc")
    public BaseRespVo unAuthc(){
        return BaseRespVo.error(601,"没有登录，请先登录");
    }
}
